
#include <set>
#include <JSL.hpp>

#include "../../debug.hpp"
#include "../../math.hpp"

#include "AnimationFramer.hpp"

namespace zen {

	namespace video {

		namespace model {


			AnimationFramer::AnimationFramer() {
				type = AnimTypeNone;
				start = 0;
				end = 0;
				position = 0;
				speed = 1.0f;
				paused = false;
				ppdir = false;
			}

			AnimationFramer::AnimationFramer(const AnimationFramer &other) {
				*this = other;
			}

			AnimationFramer::~AnimationFramer() {
			}

			AnimationFramer& AnimationFramer::operator = (const AnimationFramer &other) {
				type = other.type;
				start = other.start;
				end = other.end;
				position = other.position;
				speed = other.speed;
				paused = other.paused;
				ppdir = other.ppdir;
				return *this;
			}

			bool AnimationFramer::update(int interval) {
				if (paused || type == AnimTypeNone)
					return true;
				switch (type) {
					case AnimTypeOnce:
						position += (int)(interval * speed);
						if (position > end) {position = end; paused = true;}
						break;
					case AnimTypeOnceBack:
						position -= (int)(interval * speed);
						if (position < start) {position = start; paused = true;}
						break;
					case AnimTypeLoop:
						position += (int)(interval * speed);
						while (position >= end) position -= (end - start);
						break;
					case AnimTypeLoopBack:
						position -= (int)(interval * speed);
						while (position < start) position += (end - start);
						break;
					case AnimTypePingPong:
						if (ppdir) {
							position -= (int)(interval * speed);
							if (position < start) {position = start + start - position; ppdir = false;}
						} else {
							position += (int)(interval * speed);
							if (position >= end) {position = end + end - position; ppdir = true;};
						}
						break;
					case AnimTypePingPongOnce:
						if (ppdir) {
							position -= (int)(interval * speed);
							if (position < start) {position = start; ppdir = false; paused = true;}
						} else {
							position += (int)(interval * speed);
							if (position >= end) {position = end - (position - end); ppdir = true;};
						}
						break;
				}
				return true;
			}


		} // namespace model

	} // namespace video

} // namespace zen
